package com.yzt.dynamicmodule.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
import com.baidu.mapapi.search.sug.SuggestionResult;
import com.baidu.mapapi.search.sug.SuggestionSearch;
import com.baidu.mapapi.search.sug.SuggestionSearchOption;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.yzt.zhuju.base.BaseActivity;
import com.yzt.dynamicmodule.R;
import com.yzt.dynamicmodule.adapter.GameAddCityAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：bobwang
 * 创建时间：2023-11-19 21:14:51
 * 描述：组局发布选择城市
 */
public class GameAddCityActivity extends BaseActivity {

    private GameAddCityAdapter adapter;

    private RecyclerView recyclerView;

    private final SuggestionSearch mSuggestionSearch = SuggestionSearch.newInstance();

    private EditText et_content;

    private String city = "";

    private double latitude, longitude;

    @Override
    public int getLayoutId() {
        return R.layout.activity_game_add_city;
    }

    @Override
    public void installViews() {
        city = getIntent().getStringExtra("extra_id");
        latitude = getIntent().getDoubleExtra("extra_latitude", 0);
        longitude = getIntent().getDoubleExtra("extra_longitude", 0);

        et_content = findViewById(R.id.et_content);
        mSuggestionSearch.setOnGetSuggestionResultListener(listener);

        recyclerView = findViewById(R.id.recycler_view);
        // 单页加载控件
        LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new GameAddCityAdapter();
        adapter.setPlaceLatLng(latitude, longitude);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter1, @NonNull View view, int position) {
                for (PoiInfo bean : adapter.getData()) {
                    bean.setPano(false);
                }
                adapter.getItem(position).setPano(true);
                PoiInfo bean = adapter.getItem(position);
                Intent intent = new Intent();
                intent.putExtra("extra_data", bean);
                setResult(RESULT_OK, intent);
                finish();
            }
        });

        et_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String content = et_content.getText().toString().trim();
                if (!content.equals("")) {
                    searchData(content);
                }
            }
        });
    }

    /**
     * 执行搜索功能
     */
    private void searchData(String keyword) {
        mSuggestionSearch.requestSuggestion(new SuggestionSearchOption()
                .city(city)
                .keyword(keyword));
    }

    OnGetSuggestionResultListener listener = new OnGetSuggestionResultListener() {
        @Override
        public void onGetSuggestionResult(SuggestionResult suggestionResult) {
            //处理sug检索结果
            updateSearch(suggestionResult);
        }
    };

    /**
     * 更新UI
     */
    private void updateSearch(SuggestionResult suggestionResult) {
        List<SuggestionResult.SuggestionInfo> list = suggestionResult.getAllSuggestions();
        List<PoiInfo> poiInfos = new ArrayList();
        for (SuggestionResult.SuggestionInfo bean : list) {
            PoiInfo poiInfo = new PoiInfo();
            poiInfo.address = bean.getAddress();
            poiInfo.location = bean.getPt();
            poiInfo.name = bean.getKey();
            poiInfos.add(poiInfo);
        }
        adapter.getData().clear();
        adapter.addData(poiInfos);
    }
}
